package edu.sdjzu.exam.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import edu.sdjzu.exam.common.CommonResponse;
import edu.sdjzu.exam.dto.ExamCreateDTO;
import edu.sdjzu.exam.dto.ExamDTO;
import edu.sdjzu.exam.dto.ExamUpdateDTO;
import edu.sdjzu.exam.dto.StudentExamDTO;
import edu.sdjzu.exam.service.exam.ExamService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/exams")
@RequiredArgsConstructor
@SaCheckLogin
public class ExamController {

    private final ExamService examService;

    @PostMapping("/create")
    public CommonResponse<Long> createExam(@Valid @RequestBody ExamCreateDTO createDTO) {
        try {
            Long examId = examService.createExam(createDTO);
            return CommonResponse.success("试卷创建成功", examId);
        } catch (Exception e) {
            return CommonResponse.error("创建失败：" + e.getMessage());
        }
    }

    @GetMapping("/{examId}")
    @SaCheckRole("teacher")
    public CommonResponse<ExamDTO> getExam(@PathVariable Long examId) {
        try {
            ExamDTO examDTO = examService.getExam(examId);
            return CommonResponse.success(examDTO);
        } catch (Exception e) {
            return CommonResponse.error("获取失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/{examId}/student")
    @SaCheckRole("student")
    public CommonResponse<StudentExamDTO> getStudentExam(@PathVariable Long examId) {
        try {
            StudentExamDTO examDTO = examService.getStudentExam(examId);
            return CommonResponse.success(examDTO);
        } catch (Exception e) {
            return CommonResponse.error("获取失败：" + e.getMessage());
        }
    }

    @PutMapping("/{examId}")
    @SaCheckRole("teacher")
    public CommonResponse<String> updateExam(@PathVariable Long examId, @Valid @RequestBody ExamUpdateDTO updateDTO) {
        try {
            // 验证路径ID与请求体ID是否匹配
            if(!examId.equals(updateDTO.getExamId())) {
                throw new IllegalArgumentException("路径ID与请求体ID不匹配");
            }
            examService.updateExam(updateDTO);
            return CommonResponse.success("试卷更新成功");
        } catch (Exception e) {
            return CommonResponse.error("更新失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/{examId}")
    @SaCheckRole("teacher")
    public CommonResponse<String> deleteExam(@PathVariable Long examId) {
        try {
            examService.deleteExam(examId);
            return CommonResponse.success("试卷删除成功");
        } catch (Exception e) {
            return CommonResponse.error("删除失败：" + e.getMessage());
        }
    }

    @GetMapping("/my")
    @SaCheckRole(value = {"teacher", "student"}, mode = SaMode.OR)
    public CommonResponse<List<ExamDTO>> listMyExams() {
        try {
            List<ExamDTO> exams = examService.listExamsByCreator();
            return CommonResponse.success(exams);
        } catch (Exception e) {
            return CommonResponse.error("获取失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/{examId}/total-score")
    @SaCheckRole("teacher")
    public CommonResponse<java.math.BigDecimal> getTotalScore(@PathVariable Long examId) {
        try {
            java.math.BigDecimal totalScore = examService.calculateTotalScore(examId);
            return CommonResponse.success("获取试卷总分成功", totalScore);
        } catch (Exception e) {
            return CommonResponse.error("获取失败：" + e.getMessage());
        }
    }

    @Data
    public static class ExamIdDTO {
        @NotNull(message = "试卷ID不能为空")
        private Long examId;
    }
}
